<template>
  <form-view class="fault-view-form-view bbx-cell-form-view" :class="formCellCount > 1 ? 'bbx-cell-form-view-split' : ''" :form-cell-count="formCellCount" :fields="fields" :value="ruleForm" />
</template>
        
<script>
import { t } from '@src/locales'
import * as FormUtil from '@src/component/form/util'
// api
import { 
  faultDetail, // 获取故障对象/故障描述详情数据
  solutionGetFields, // 获取解决方案表单字段
  solutionGetData, // 获取解决方案的详细数据 
} from '@src/api/Fault';
// todo shanchu 
import { mock1, mock2,solutionGetFields1,solutionSearch1 } from '@src/modules/faultLibrary/mode/solutionGetFields.js';

const separatorSystem = {
  id:'separatorSystem',
  displayName: t('common.base.basicInfo'),
  formType: 'separator'
};
const separatorBase = {
  id:'separatorBase',
  displayName: t('common.form.fieldGroupName.system2'),
  formType: 'separator'
};
let systemFields = [{
  'id': 3,
  'isSystem': 1,
  'fieldName': 'createName',
  'displayName': t('common.base.createUser'),
  'formType': 'text',
}, {
  'id': 4,
  'isSystem': 1,
  'fieldName': 'createTime',
  'displayName': t('common.base.createTime'),
  'formType': 'text',
}, {
  'id': 5,
  'isSystem': 1,
  'fieldName': 'updateName',
  'displayName': t('common.base.column.lastUpdatePerson'),
  'formType': 'text',
}, {
  'id': 6,
  'isSystem': 1,
  'fieldName': 'updateTime',
  'displayName': t('common.base.lastUpdateTime'),
  'formType': 'text',
}];
// const bianhao = {
//   'id': 1,
//   'isSystem': 1,
//   'fieldName': 'id',
//   'displayName': t('common.base.serialNumber'),
//   'formType': 'text',
// }
const baseFields = [{
  'id': 2,
  'isSystem': 1,
  'fieldName': 'faultDesc',
  'displayName': t('common.base.title'),
  'formType': 'text',
}, {
  'id': 3,
  'isSystem': 1,
  'fieldName': 'faultContent',
  'displayName': t('common.base.description1'),
  'formType': 'text',
}, {
  'id': 4,
  'isSystem': 1,
  'fieldName': 'attachment',
  'displayName': t('common.paas.formType.attachment'),
  'formType': 'attachment',
},{
  'id': 5,
  'isSystem': 1,
  'fieldName': 'relObj',
  'displayName': t('product.relateProduct'),
  'formType': 'text',
}]
export default{
  // 故障详情页面的左边内容
  name:'FaultView',
      
  data(){
    return {
      // 需要调用的接口
      promisePort:[],
      // 字段列表
      fields: [],
      // 表单字段值
      ruleForm:{}
    }
  },

  props:{
    description:{
      type: String
    },
    query:{
      type:Object
    },
    formCellCount: {
      type: Number,
      default: 1
    },
  },
    
  mounted(){
    const {type, id} = this.query 
    this.promisePort = []

    // 解决方案,需要获取表单字段和数据详情两个接口
    if(type === 'solution'){
      // 获取详情数据
      this.promisePort = [ solutionGetFields, () => { return solutionGetData({solutionId: id}) }]
    }else{
      // todo 此处solutionGetFields换获取故障对象/故障描述的接口
      this.promisePort = [ this.getFaultDescriptionFields, () => { return faultDetail({faultId: id}).then(res=>{
        if(res.status === 0){
          let attribute = res.data.attribute || {}
          res.data = {
            ...res.data,
            ...attribute,
          }
          return res
        }
      }) } ]
    }
  },
      
  methods: {
    /**
     * @description 获取Field
     */
    getField(fields, type){
      let field = fields?.filter(item => item.formType == type)[0]
      return field;
    },
    // 供父组件调用，接口成功后的回调
    async setData(res){
      if(this.query.type === 'solution' ){
        const res1Data = res[1]?.data || {};
        const attribute = res1Data.attribute || {}
        res[1].data = {
          ...res1Data,
          solution_desc: res1Data.solutionDesc,
          wikiDataValue: res1Data.wikiDataValue,
          sparePartValue: res1Data.sparePartValue,
          serviceProjectsValue: res1Data.serviceProjectsValue,
          materialValue: res1Data.materialValue,
          attribute
        }
        // todo 
        // res[0].data = [ bianhao, ...solutionGetFields1.data ]
      }
      
      baseFields[3].displayName = t('faultLibrary.faultObject.label8')
      this.fields = [ separatorSystem, ...res[0].data, separatorBase, ...systemFields ]
      // 处理富文本
      res[1].data = await FormUtil.initRichTextContent(this.fields, res[1].data)

      this.ruleForm = res[1].data,
      this.ruleForm.relObj = this.ruleForm.relObj?.map(item => item.relObjName).join(',') || ''
      this.$emit('headerTitle', this.ruleForm?.faultDesc || '');
    },

    // 供父组件调用，获取表单字段和表单元素值的接口数据
    getPortPromise(){
      return new Promise((resolve) => {
        Promise
          .all( this.promisePort.map( item => { return item() }))
          .then((res) => {
            resolve(res)
          })
      })
    },

    // 供父组件调用，用于重新获取表单元素值的数据
    // async getFieldsValue(){
    //   const { id } = this.query || {}
    //   const res = await solutionGetData({solutionId: id});
    //   if(res.status !== 0) return 
    //   this.ruleForm = res.data || {}
    // },

    // 封装成异步(为了减少判断)，返回故障对象/故障描述的表单字段
    getFaultDescriptionFields(){
      return new Promise((resolve) => {
        resolve({
          status:0,
          data: baseFields
        })
      })
    }
  }
}
</script>
  
<style lang="scss" scoped>
.fault-view-form-view ::v-deep{
  .view-group{
    padding:0 8px;
  }
}
</style>